"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : autocommit
Case Name   : 关闭自动提交，测试显式开启事务，发出警告提示
Create At   : 2023/01/17
@zou_jialiang0501162244
Description :
    1、关闭自动提交后start transaction显式开启事务
    2、关闭自动提交后begin显式开启事务
    3、关闭自动提交后执行建表语句后显示开启事务，回滚
    4、清理环境
Expect      :
    1、有WARNING信息
    2、有WARNING信息
    3、有WARNING信息，回滚成功
    4、成功
History     :
"""

import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class TestAutocommit(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.tb_name = 't_autocommit_0018'
        self.db_name = 'db_autocommit_0018'
        self.war_msg = 'WARNING:  there is already a transaction in progress'
        self.log.info("-----预置兼容B库-----")
        preset_db = f"drop database if exists {self.db_name};" \
                    f"create database {self.db_name} dbcompatibility 'B';"
        preset_db_res = self.pri_sh.execut_db_sql(preset_db)
        self.log.info(preset_db_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, preset_db_res,
                      f"创建兼容B库失败")

    def test_autocommit(self):
        text1 = '-----step1:关闭自动提交后start transaction显式开启事务;' \
                'expect:有WARNING信息-----'
        self.log.info(text1)
        sql1 = f"set autocommit to off;" \
               f"start transaction;" \
               f"set autocommit to on;"
        sql1_res = self.pri_sh.execut_db_sql(sql1, dbname=self.db_name)
        self.log.info(sql1_res)
        self.assertEqual(sql1_res.count('SET'), 2, f"执行失败: {text1}")
        self.assertIn(self.war_msg, sql1_res, f"执行失败: {text1}")
        self.assertIn(self.constant.START_TRANSACTION_SUCCESS_MSG, sql1_res,
                      f"执行失败: {text1}")

        text2 = '-----step2:关闭自动提交后begin显式开启事务;' \
                'expect:有WARNING信息-----'
        self.log.info(text2)
        sql2 = f"set autocommit to false;" \
               f"begin;" \
               f"set autocommit to true;"
        sql2_res = self.pri_sh.execut_db_sql(sql2, dbname=self.db_name)
        self.log.info(sql2_res)
        self.assertEqual(sql2_res.count('SET'), 2, f"执行失败: {text2}")
        self.assertIn(self.war_msg, sql2_res, f"执行失败: {text2}")

        text3 = '-----step3:关闭自动提交，执行建表语句后显示开启事务;' \
                'expect:有WARNING信息，回滚成功-----'
        self.log.info(text3)
        sql3 = f"set autocommit = 0;" \
               f"create table {self.tb_name}(a int unsigned);" \
               f"begin;" \
               f"rollback;"
        sql3_res = self.pri_sh.execut_db_sql(sql3, '', self.db_name)
        self.log.info(sql3_res)
        self.assertIn('SET', sql2_res, f"执行失败: {text3}")
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, sql3_res,
                      f"建表失败: {text3}")
        self.assertIn(self.war_msg, sql3_res, f"执行失败: {text3}")
        self.assertIn(self.constant.ROLLBACK_MSG, sql3_res, f"执行失败: {text3}")

    def tearDown(self):
        text4 = '-----step4:清理环境 expect:成功-----'
        self.log.info(text4)
        drop_db_res = self.pri_sh.execut_db_sql(
            f"drop database if exists {self.db_name};")
        self.log.info(drop_db_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_db_res,
                      f"执行失败: {text4}")
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
